package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.CheckUtil;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.KeyBoardUtils;
import com.dlwx.firefighting.utils.MD5Util;
import com.dlwx.firefighting.utils.TimeCount;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPswActivity extends BaseActivity {

    @BindView(R.id.register_back)
    ImageView registerBack;
    @BindView(R.id.register_edit_phone)
    EditText registerEditPhone;
    @BindView(R.id.register_yzm)
    TextView registerYzm;
    @BindView(R.id.register_edit_yzm)
    EditText registerEditYzm;
    @BindView(R.id.register_edit_psw)
    EditText registerEditPsw;
    @BindView(R.id.et_confirm_psw)
    EditText etConfirmPsw;
    @BindView(R.id.button_register)
    TextView buttonRegister;
    private TimeCount timeCount;
    private String phoneNum;
    private String yzm;
    private String psw,confirmPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_phone);
        ButterKnife.bind(this);

        initTitle();
        //倒计时
        timeCount = new TimeCount(registerYzm, 120*1000, 1000);
    }

    private void initTitle() {

    }

    private void confirm() {
        psw=MD5Util.getMD5(psw);
        OkGo.<String>post(Constant.FIND_PSW)
                .tag(this)
                .params("phoneNumber", phoneNum)
                .params("userPwd", psw)
                .params("vcode", yzm)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        int code =  DealResult.getInstance().deal(FindPswActivity.this, response);
                        if(code==Constant.SUCESS){
                            FindPswActivity.this.finish();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForLong(FindPswActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    @OnClick({R.id.register_back, R.id.register_yzm, R.id.button_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_back:
                this.finish();
                break;
            case R.id.register_yzm:
                phoneNum= registerEditPhone.getText().toString();
                if (CheckUtil.isPhoneNum(phoneNum) && phoneNum.length() == 11) {
                    getYzm();
                }else {
                    registerEditPhone.setText("");
                    ToastUtils.toastForLong(FindPswActivity.this, "手机号码格式错误");
                }
                break;
            case R.id.button_register:
                if (checkUserInfo()) {
                    KeyBoardUtils.closeKeybord(FindPswActivity.this);
                    confirm();
//                    ToastUtils.toastForLong(RegisterActivity.this, "注册");
                }
                break;
        }
    }

    private void getYzm() {
        OkGo.<String>post(Constant.YZM_FIND)
                .tag(this)
                .params("phoneNumber",phoneNum)
                  /*   1、注册 2、找回密码*/
                .params("type",1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        int code =  DealResult.getInstance().deal(FindPswActivity.this, response);
                        if(code==Constant.SUCESS){
                            timeCount.start();
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForLong(FindPswActivity.this,  getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
    /**
     * 检查用户名和密码格式
     */
    private boolean checkUserInfo() {
        phoneNum = registerEditPhone.getText().toString();
        psw = registerEditPsw.getText().toString();
        confirmPsw = etConfirmPsw.getText().toString();
        yzm= registerEditYzm.getText().toString();
        if (!CheckUtil.isPhoneNum(phoneNum)) {
            registerEditPhone.setText("");
            ToastUtils.toastForLong(FindPswActivity.this, "手机号码格式错误");
            return false;
        }

        if (yzm.length() < 6) {
            registerEditYzm.setText("");
            ToastUtils.toastForLong(FindPswActivity.this, "验证码错误");
            return false;
        }
        if(!TextUtils.equals(psw,confirmPsw)){
            ToastUtils.toastForLong(FindPswActivity.this, "2次输入的密码不一致");
            return false;
        }
        if (!CheckUtil.checkPassword(psw) || psw.length() < 6) {
            registerEditPsw.setText("");
            ToastUtils.toastForLong(FindPswActivity.this, "密码格式错误");
            return false;
        }
        if (!CheckUtil.checkPassword(confirmPsw) || confirmPsw.length() < 6) {
            etConfirmPsw.setText("");
            ToastUtils.toastForLong(FindPswActivity.this, "密码格式错误");
            return false;
        }
        return true;
    }
}
